//
//  LBWStatusCell.swift
//  weibo
//
//  Created by Limonwei on 15/12/9.
//  Copyright © 2015年 heima. All rights reserved.
//
//微博cell可分为几个部分 原创 转发 评论 配图

import UIKit
import SnapKit

// cell 内部子控件共同的间距
let LBWStatusCellMargin: CGFloat = 10

//昵称,时间,来源 字体大小
let LBWStatusNameFont: CGFloat = 14
let LBWStatusTimeFont: CGFloat = 10
let LBWStatusSourceFont: CGFloat = 10

//微博内容字体
let LBWContentFont: CGFloat = 15


class LBWStatusCell: UITableViewCell {
    
    //toolBar 顶部的约束  记录一下 后面调用
    var toolBarTopConstraint: Constraint?
    
    var statusViewModel: LBWStatusViewModel? {
        
        didSet{
            //填充控件内容
            originalView.statusViewModel = statusViewModel
            
            statusToolBar.statusViewModel = statusViewModel
            
            //先移除以前toolBar顶部的约束
            toolBarTopConstraint?.uninstall()
            
            //更新约束
            //如果有转发微博,让toolBar的顶部等于转发微博的底部
            if statusViewModel?.status?.retweeted_status != nil {
                
                //显示
                retweetView.hidden = false
                retweetView.statusViewModel = statusViewModel
                
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            }else{
                //如果没有转发微博,就让其顶部等于原创微博的底部
                
                //隐藏
                retweetView.hidden = true
                
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置视图
    private func setupUI(){
        
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        //添加View  cell 里面要添加到contentView
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(statusToolBar)
        
        
        //添加约束
        //原创微博
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.top.equalTo(contentView).offset(8)
            make.trailing.equalTo(contentView)
        }
        
        //转发微博
        retweetView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(originalView)
            make.top.equalTo(originalView.snp_bottom)
            make.trailing.equalTo(originalView)
        }
        //底部
        statusToolBar.snp_makeConstraints { (make) -> Void in
            self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            
            make.leading.equalTo(contentView)
            make.width.equalTo(contentView)
            make.height.equalTo(35)
        }
        
        //!!!
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(statusToolBar)
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
        }
        
        
    }
    
    //懒加载视图
    //原创微博视图部分
    private lazy var originalView: LBWStatusOriginalView = {
    
    
        let view = LBWStatusOriginalView()
        
        view.backgroundColor = UIColor.whiteColor()
        
        return view
    
        
    }()
    //转发微博视图部分
    private lazy var statusToolBar: LBWStatusToolBar = LBWStatusToolBar()
    //底部工具条视图部分
    private lazy var retweetView: LBWStatusRetweetView = LBWStatusRetweetView()

}












